home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / print / gs261sr1.zip / GDEV3B1.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  22KB  |  799 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /*
  20.  * gdev3b1.c
  21.  *
  22.  * This is a driver for the AT&T 3b1/7300/UnixPC console display.
  23.  *
  24.  * The image is built in a buffer the size of the page.  Once complete,
  25.  * a screen-sized subset is copied to the screen, and one can scroll
  26.  * through the entire image (move with "vi" or arrow keys).
  27.  *
  28.  * Written by Andy Fyfe, andy@cs.caltech.edu.
  29.  *
  30.  * There are a couple of undesirable "features" that I have found no
  31.  * way to work around.
  32.  *
  33.  * 1) Gs attempts to save the contents of the window before using it, and
  34.  *    then restores the contents afterward.  However, if the gs window is
  35.  *    not the current window, and there are small windows present, then
  36.  *    the saved image is incorrect, and thus the screen will not be correctly
  37.  *    restored.  This seems to be a bug in the 3b1 window driver.  Making
  38.  *    the gs window current before saving its contents is not an acceptable
  39.  *    solution.
  40.  *
  41.  * 2) Gs will enable the scrolling/help/cancel icons if the window has
  42.  *    a border.  Changing these border icons has the side effect of making
  43.  *    the gs window current.  This does circumvent the first problem though.
  44.  */
  45.  
  46. /*
  47.  * About the ATT3B1_PERF flag (notes by Andy Fyfe):
  48.  *
  49.  * I am unable to profile gs on the 3b1, so I added ATT3B1_PERF as a
  50.  * quick way to find out how much time was spent in the 3b1 driver,
  51.  * through dynamically suppressing parts of the code at run time by
  52.  * setting environment variables.  I can then get the time spent in
  53.  * those parts by comparing the results of "time gs ....".
  54.  *
  55.  * At one point this was very useful, and led to a fairly substantial
  56.  * speedup of the fill and copy_mono routines.  It also showed that I
  57.  * wasn't going to get too much more, overall, by further attempts to
  58.  * optimize the 3b1 driver.  So those parts of the code controlled by
  59.  * ATT3B1_PERF have really now outlived their usefulness.
  60.  */
  61.  
  62. #include "gx.h"
  63. #include "gxdevice.h"
  64. #include "gserrors.h"
  65.  
  66. #include <errno.h>
  67. #include <sys/window.h>
  68. #include <sys/termio.h>
  69.  
  70. typedef struct gx_device_att3b1_s {
  71.     gx_device_common;
  72.     int fd;                /* window file descriptor */
  73.     uchar *screen;            /* pointer to screen image */
  74.     ushort line_size;            /* size of screen line in bytes */
  75.     ulong screen_size;            /* size of screen image in bytes */
  76.     int page;                /* page number */
  77. #ifdef ATT3B1_PERF
  78.     char *no_output, *no_fill, *no_copy;
  79. #endif
  80. } gx_device_att3b1;
  81. #define att3b1dev ((gx_device_att3b1 *)dev)
  82.  
  83. #define XDPI    100        /* to get a more-or-less square aspect ratio */
  84. #define YDPI    72
  85. #define XSIZE (8.5 * XDPI)    /* 8.5 x 11 inch page, by default */
  86. #define YSIZE (11 * YDPI)
  87.  
  88. static const ushort masks[] = { 0,
  89.     0x0001, 0x0003, 0x0007, 0x000f,
  90.     0x001f, 0x003f, 0x007f, 0x00ff,
  91.     0x01ff, 0x03ff, 0x07ff, 0x0fff,
  92.     0x1fff, 0x3fff, 0x7fff, 0xffff,
  93. };
  94. static uchar reverse_bits[256] = {
  95.   0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
  96.   8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
  97.   4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
  98.   12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
  99.   2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
  100.   10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
  101.   6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
  102.   14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
  103.   1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
  104.   9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
  105.   5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
  106.   13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
  107.   3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
  108.   11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
  109.   7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
  110.   15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
  111. };
  112.  
  113. dev_proc_open_device(att3b1_open);
  114. dev_proc_close_device(att3b1_close);
  115. dev_proc_fill_rectangle(att3b1_fill_rectangle);
  116. dev_proc_copy_mono(att3b1_copy_mono);
  117. dev_proc_output_page(att3b1_output_page);
  118.  
  119. private gx_device_procs att3b1_procs = {
  120.     att3b1_open,
  121.     gx_default_get_initial_matrix,
  122.     gx_default_sync_output,
  123.     att3b1_output_page,
  124.     att3b1_close,
  125.     gx_default_map_rgb_color,
  126.     gx_default_map_color_rgb,
  127.     att3b1_fill_rectangle,
  128.     gx_default_tile_rectangle,
  129.     att3b1_copy_mono,
  130.     gx_default_copy_color,
  131.     gx_default_draw_line,
  132.     gx_default_get_bits,
  133.     gx_default_get_props,
  134.     gx_default_put_props
  135. };
  136.  
  137. gx_device_att3b1 gs_att3b1_device = {
  138.     sizeof(gx_device_att3b1),
  139.     &att3b1_procs,
  140.     "att3b1",
  141.     XSIZE, YSIZE,
  142.     XDPI, YDPI,
  143.     no_margins,
  144.     dci_black_and_white,
  145.     0,
  146.     -1, 0, 0,            /* fd, screen, line_size, */
  147.     0, 0,            /* screen size, page */
  148. #ifdef ATT3B1_PERF
  149.     0, 0, 0,            /* no_output, no_fill, no_copy */
  150. #endif
  151. };
  152.  
  153. int
  154. att3b1_open(gx_device *dev)
  155. {
  156.     struct uwdata uw;
  157.  
  158. #ifdef ATT3B1_PERF
  159.     char *getenv(const char *);
  160. #endif
  161.  
  162.     if (att3b1dev->fd >= 0) {
  163.     close(att3b1dev->fd);
  164.     att3b1dev->fd = -1;
  165.     }
  166.  
  167.     if (att3b1dev->screen != NULL) {
  168.     gs_free((char *)att3b1dev->screen,
  169.         att3b1dev->screen_size, 1, "att3b1_open");
  170.     att3b1dev->screen = 0;
  171.     att3b1dev->screen_size = 0;
  172.     }
  173.  
  174.     att3b1dev->fd = open("/dev/tty", 2);
  175.     if (att3b1dev->fd < 0) {
  176.     lprintf1("att3b1_open: open /dev/tty failed [%d]\n", errno);
  177.     return_error(gs_error_ioerror);
  178.     }
  179.  
  180.     /* Verify that /dev/tty is associated with a console window. */
  181.     if (ioctl(att3b1dev->fd, WIOCGETD, &uw) < 0) {
  182.     lprintf1("att3b1_open: can not obtain window data [%d]\n", errno);
  183.     lprintf("att3b1_open: the att3b1 device requires a console window\n");
  184.     att3b1_close(dev);
  185.     return_error(gs_error_ioerror);
  186.     }
  187.  
  188.     /* we need an even number of bytes per line */
  189.     att3b1dev->line_size = ((att3b1dev->width + 15) / 16) * 2;
  190.     att3b1dev->screen_size = att3b1dev->line_size * att3b1dev->height;
  191.  
  192.     att3b1dev->screen =
  193.     (uchar *)gs_malloc(att3b1dev->screen_size, 1, "att3b1_open");
  194.     if (att3b1dev->screen == NULL) {
  195.     att3b1_close(dev);
  196.     return_error(gs_error_VMerror);
  197.     }
  198.  
  199.     att3b1dev->page = 1;
  200.  
  201. #ifdef ATT3B1_PERF
  202.     att3b1dev->no_output = getenv("GS_NOOUTPUT");
  203.     att3b1dev->no_fill = getenv("GS_NOFILL");
  204.     att3b1dev->no_copy = getenv("GS_NOCOPY");
  205. #endif
  206.  
  207.     return 0;
  208. }
  209.  
  210. int
  211. att3b1_close(gx_device *dev)
  212. {
  213.     if (att3b1dev->fd >= 0) {
  214.     close(att3b1dev->fd);
  215.     att3b1dev->fd = -1;
  216.     }
  217.  
  218.     if (att3b1dev->screen != NULL) {
  219.     gs_free((char *)att3b1dev->screen,
  220.         att3b1dev->screen_size, 1, "att3b1_close");
  221.     att3b1dev->screen = 0;
  222.     att3b1dev->screen_size = 0;
  223.     }
  224.  
  225.     return 0;
  226. }
  227.  
  228. int
  229. att3b1_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  230.                       gx_color_index colour)
  231. {
  232.     uint o, b, wl, wr, w2;
  233.     ushort *p, *q, maskl, maskr;
  234.  
  235. #ifdef ATT3B1_PERF
  236.     if (att3b1dev->no_fill) return 0;
  237. #endif
  238.  
  239.     fit_fill(dev, x, y, w, h);
  240.  
  241.     /* following fit_fill, we c